const makeUrlWithParams = (url, params) => {
  if (url) {
    let result = url.trim()
    let needAnd = false
    if (url.indexOf('?') < 0) {
      result = `${url}?`
    } else if (!url.endsWith('?')) {
      needAnd = true
    }
    if (params) {
      for (const key in params) {
        if (needAnd) {
          result += '&'
        } else {
          needAnd = true
        }
        if (Object.prototype.hasOwnProperty.call(params, key)) {
          const data = typeof params[key] === 'string'
            ? params[key]
            : JSON.stringify(params[key])
          result += `${key}=${encodeURIComponent(data)}`
        }
      }
    }
    return result
  } else {
    return ''
  }
}

const CHANNEL_STATUS_MAP = {
  10: '待审核',
  20: '审核不通过',
  30: '审核通过',
  40: '作废'
}

const DOOR_STATUS_MAP = {
  10: '待发货',
  20: '发货中',
  30: '已发货',
  40: '已收货',
  50: '已结算'
}

const APPROVAL_STATUS_MAP = {
  1: '未审核',
  2: '审核失败',
  3: '审核成功'
}

const DELIVERY_TYPE_MAP = {
  1: '自提',
  2: '送货上门'
}

const getArrayFromMap = (mapObj) => {
  const mapArr = Object.keys(mapObj).map(key => {
    return {
      value: key,
      label: mapObj[key]
    }
  })
  return mapArr
}

/**
 * tree data换属性名
 * @param data
 * @returns {Array}
 */
const transformData = (data) => {
  const list = []
  if (data.length === 0) {
    return list
  }
  data.map((item) => {
    const newData = {}
    newData.key = item.categoryCode
    newData.value = item.categoryCode
    newData.label = item.categoryName
    if (item.children && item.children.length > 0) {
      newData.children = item.children ? transformData(item.children) : []
    }
    list.push(newData)
  })
  return list
}

export {
  transformData,
  getArrayFromMap,
  makeUrlWithParams,
  CHANNEL_STATUS_MAP,
  DOOR_STATUS_MAP,
  APPROVAL_STATUS_MAP,
  DELIVERY_TYPE_MAP
}
